import storage from './storage';
import { logger, UserLogOut } from './util';

export const USER_LOGIN_TOKEN_KEY = 'USER_LOGIN_TOKEN_KEY';

export async function setLoginUserToken(token) {
  if (!token || (typeof token === 'string' && !token.length)) {
    UserLogOut();
  }
  // console.log("set token:",token)
  return await storage.set(USER_LOGIN_TOKEN_KEY, token).catch(e => logger(e));
}

export async function clearLoginUserToken() {
  //无法做到同步
  // console.log("clear token")
  // return await storage.set(USER_LOGIN_TOKEN_KEY, '').catch(e => logger(e));
}

export async function getLoginUserToken(defaultValue = null) {
  return await storage.get(USER_LOGIN_TOKEN_KEY, defaultValue).catch(e => logger(e));
}

export async function hasLoginUserToken() {
  let token = await getLoginUserToken().catch(e => logger(e));
  if (token) {
    return true;
  }
  return false;
}
